 //正则
    let user = document.getElementById("user");
    let pass = document.getElementById("pass");
    let pass2 = document.getElementById("pass2");
    let mail = document.getElementById("mail");
    let tel = document.getElementById("tel");
    let btn = document.getElementById("btn");

    let passOnOff = pass2OnOff = telOnOff = userOnoff = mailOnoff = false;

    user.oninput = function(){
        var reg = /^[\w\-]{4,12}$/;
        if(reg.test(user.value)){
            this.nextElementSibling.innerHTML = "符合要求";
                    userOnoff = true;
        }else{
            this.nextElementSibling.innerHTML = "请输入字母数字_-4-12位";
                    userOnoff =false;
        }
    }

    mail.oninput = function(){
        var reg = /^[0-9a-zA-Z]{4,9}@[\da-zA-Z]{2,9}\.[a-zA-Z]{2,4}$/;
        if(reg.test(this.value)){
            this.nextElementSibling.innerHTML = "符合要求";
                mailOnoff = true;
        }else{
            this.nextElementSibling.innerHTML = "请输入正确的电子邮箱";
                mailOnoff = false;
        }
    }

    tel.oninput = function(){
        var reg = /^1[3-9]\d{9}$/;
        if(reg.test(this.value)){
            this.nextElementSibling.innerHTML = "符合";
            telOnOff = true;
        }else{
            this.nextElementSibling.innerHTML = "不符合";
            telOnOff = false;
        }
    }

    pass.oninput = function(){
        var a=b=c=0;

        var lgReg = /^.{6,18}$/;
        if(!lgReg.test(this.value)){
            this.nextElementSibling.innerHTML = "长度不符合";
            passOnOff = false;
            return ;
        }

        // 判断是否出现数字
        var numReg = /\d/;
        if(numReg.test(this.value)){
            a = 1;
        }
        // 判断是否出现字母
        var azReg = /[a-z]/i;
        if(azReg.test(this.value)){
            b = 1;
        }
        // 判断是否实现非数字非字母
        var tsReg = /[^\da-z]/i;
        if(tsReg.test(this.value)){
            c = 1;
        }
        // 做最终的统计，决定难度等级
        switch(a+b+c){
            case 1:
                this.nextElementSibling.innerHTML = "简单";break;
            case 2:
                this.nextElementSibling.innerHTML = "一般";break;
            case 3:
                this.nextElementSibling.innerHTML = "困难";break;
        }
        passOnOff = true;


        // 重复密码的判断，要双向判断：
            // 在第一个框，也要判断
            // 但是如果第二个框为空，可以暂时不判断
        if(pass2.value === "") return;

        if(this.value === pass2.value){
            pass2.nextElementSibling.innerHTML = "一致";
            pass2OnOff = true;
        }else{
            pass2.nextElementSibling.innerHTML = "不一致";
            pass2OnOff = false;
        }

    }

    // 重复密码功能，直接判断是和上一个密码重复，即可
    pass2.oninput = function(){
        if(this.value === pass.value){
            this.nextElementSibling.innerHTML = "一致";
            pass2OnOff = true;
        }else{
            this.nextElementSibling.innerHTML = "不一致";
            pass2OnOff = false;
        }
    }

    btn.onclick = function(){
        if(passOnOff && pass2OnOff && telOnOff && userOnoff && mailOnoff){
            alert("提交成功");
            return ;
        }
        if(!passOnOff){
            alert("密码不对");
        }
        if(!pass2OnOff){
            alert("重复密码不对");
        }
        if(!telOnOff){
            alert("手机号不对");
        }
        if(!userOnoff){
            alert("用户名不对");
        }
        if(!telOnOff){
            alert("手机号不对");
        }
    }

    class Register{
        constructor(){
            // 选择各种元素
            this.user = document.getElementById("user");
            this.pass = document.getElementById("pass");
            this.tel = document.getElementById("tel");
            this.mail = document.getElementById("mail");
            this.btn = document.getElementById("btn");
            this.msg = document.querySelector(".msg");
            // 提前准备接口
            this.url = "http://api.icodeilife.cn:81/user";
            // 绑定事件
            this.addEvent()
        }
        addEvent(){
            var that = this;
            this.btn.onclick = function(){
                // 注册请求之前获取输入框内容
                that.u = that.user.value;
                that.p = that.pass.value;
                that.t = that.tel.value;
                that.e = that.mail.value;
                // 开启请求
                that.load();
            }
        }
        load(){
            ajax({
                url:this.url,
                data:{      // 根据接口文档，发送数据
                    type: "register",
                    user: this.u,
                    pass: this.p,
                    tel: this.t,
                    email: this.e
                }
            }).then((res)=>{
                // 请求成功后，查看数据，处理数据
                // console.log(res);
                this.res = JSON.parse(res);
                // 渲染结果
                this.display();
            })
        }
        display(){
            // 根据成功或失败，决定后续操作
            if(this.res.code == 1){
                this.msg.innerHTML = this.res.msg + "，1秒后到登录页面";
                setTimeout(()=>{
                    location.href = "sign.html";
                },1000);
            }else{
                this.msg.innerHTML = this.res.msg;
            }
        }
    }

    new Register;